#include "main.h"

bool s1_now, s2_now, s3_now;
bool s1_last, s2_last, s3_last;
uint32_t s1_cnt, s2_cnt, s3_cnt;
uint8_t sNow;
bool s1_yes;
bool s2_yes;
bool s3_yes;

void sensor_init(void)
{

	GPIO_Init(SENSOR_PORT, SENSOR_1|SENSOR_2|SENSOR_3, GPIO_MODE_IN_FL_NO_IT);

	s1_cnt = 0;
	s2_cnt = 0;
	s3_cnt = 0;

	s1_yes = FALSE;
	s2_yes = FALSE;
	s3_yes = FALSE;

	s1_last = FALSE;
	s2_last = FALSE;
	s3_last = FALSE;

}

void sensor_check(void)
{
	sNow = 0xFF - GPIO_ReadInputData(SENSOR_PORT); // if pull-up

	s1_now = (sNow & SENSOR_1) ;
	s2_now = (sNow & SENSOR_2) ;
	s3_now = (sNow & SENSOR_3) ;

	if (s1_now) {
		s1_yes = TRUE;
	}else{
		if (s1_yes) {
			eeprom_savetime(1);
			ShowRecord(sensor_save_cnt);
			s1_yes = FALSE;
		}
	}

	if (s2_now) {
		s2_yes = TRUE;
	}else{
		if (s2_yes) {
			eeprom_savetime(2);
			ShowRecord(sensor_save_cnt);
			s2_yes = FALSE;
		}
	}

	if (s3_now) {
		s3_yes = TRUE;
	}else{
		if (s3_yes) {
			eeprom_savetime(3);
			ShowRecord(sensor_save_cnt);
			s3_yes = FALSE;
		}
	}

	if (s1_now|s2_now|s3_now) {
		GPIO_WriteLow(GPIOB, LED_SIGNAL);
	}else{
		GPIO_WriteHigh(GPIOB, LED_SIGNAL);
	}

}

